﻿using Blog.Entity.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Blog.EntityFrameWork.EntityFramework.Repositories
{
    public interface IRepository<TEntity, TPrimaryKey> where TEntity : class , IEntity<TPrimaryKey>
    {
        /// <summary>
        /// 新增并且返回ID
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<TPrimaryKey> CreateAndGetIdAsync(TEntity entity);

        /// <summary>
        /// 返回满足条件的第一条记录
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task<TEntity> FirstOrDefultAsync(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 返回满足条件的第一条记录
        /// </summary>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        Task<TEntity> FirstOrDefultAsync(TPrimaryKey primaryKey);

        /// <summary>
        /// 获取所有记录
        /// </summary>
        /// <returns> IQueryable<T> </returns>
        IQueryable<TEntity> GetAll();

        /// <summary>
        /// 获取所有记录
        /// </summary>
        /// <returns> List<T> </returns>
        Task<List<TEntity>> GetAllListAsync();

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        Task Remove(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task Remove(TEntity entity);

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<TEntity> UpdateAsync(TEntity entity);

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="key"></param>
        /// <param name="updateAction"></param>
        /// <returns></returns>
        Task<TEntity> UpdateAsync(TPrimaryKey key, Func<TEntity, Task> updateAction);
    }
}
